﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web;

namespace Yuanlin.Business
{
    public class ProductProvided : YuanlinUserControl,
        IReplyConverter<CompanyProductReply, GetOperation>,
        IReplyConverter<CompanyProductReply, GetListOperation>
    {
        protected Repeater list;
        private CompanyProduct companyProduct;
        private EntityList companyProducts;
        private string title = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.Title);
        private string pinNameTitle = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.PinNameTitle);
        private string guiGeNames = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.GuiGeNames);
        private string guiGeUnits = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.GuiGeUnits);

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetCompanyProductRequest(this));

            //本公司、常用名、品名、规格、非求购
            CompanyProductListRequest request = new CompanyProductListRequest();
            request.NoUserId = UserIdFromGreenNum;
            request.PinNameTitle = pinNameTitle;
            request.Title = title;
            request.GuiGeNames = guiGeNames;
            request.GuiGeUnits = guiGeUnits;
            request.NoQiuGou = "非求购";
            request.PageIndex = 0;
            request.PageSize = 5;
            requestBinders.Add(RequestHelper.BuildGetCompanyProductRequest(this));
        }

        private void BindData()
        {
            if (list == null)
            {
                return;
            }
            if (companyProduct == null)
            {
                return;
            }

            list.DataSource = companyProducts;
            list.DataBind();
        }


        #region IReplyConverter<CompanyProductReply,GetOperation> Members

        public void ConvertReply(CompanyProductReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                companyProduct = reply.Entity as CompanyProduct;

                if (!Page.IsPostBack)
                {
                    BindData();
                }
            }
        }

        #endregion

        #region IReplyConverter<CompanyProductReply,GetListOperation> Members

        public void ConvertReply(CompanyProductReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                companyProducts = reply.EntityList;
            }
        }

        #endregion
    }
}
